﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using SBPweb.Modules.Smartparts;
using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Tools.WF;

namespace SBPweb.MediaCenter.Modules.Player.WF
{
    public partial class PlayerControl : UserControl, ISmartpart
    {

        #region Private fields

        private bool pIsModal;
        private Aligning pAlign;
        private IModule pParentModule;
        private Timer mTimer;

        #endregion

        #region Constructor logic

        public PlayerControl(IModule parentModule)
        {
            pParentModule = parentModule;
            InitializeComponent();
            Align = Aligning.Fill;

            System.Threading.Thread backThread = new System.Threading.Thread(new System.Threading.ThreadStart(TrackbarUpdaterThread));
            backThread.IsBackground = true;
            backThread.Start();


            tbBar.ValueChanged += new EventHandler(tbBar_ValueChanged);

            pMediaPlayer.uiMode = "none";
            pMediaPlayer.enableContextMenu = false;
            pMediaPlayer.stretchToFit = true;
            pMediaPlayer.settings.volume = 100;
            pMediaPlayer.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(pMediaPlayer_PlayStateChange);

            btPlay.Click += new EventHandler(btplay_Click);
            btPlay.Enabled = false;
            btStop.Click += new EventHandler(btStop_Click);
            btStop.Enabled = false;
            btPause.Click += new EventHandler(btPause_Click);
            btPause.Enabled = false;

            VisibleChanged += new EventHandler(PlayerControl_VisibleChanged);
        }


        #endregion

        #region Property accessors

        public Aligning Align
        {
            get
            {
                return pAlign;
            }
            set
            {
                pAlign = value;
                Dock = AligningConverter.AligningToDockStyle(value);
            }
        }

        public IModule ParentModule
        {
            get
            {
                return pParentModule;
            }
        }

        public bool IsVisible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
            }
        }

        public bool IsEnabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                base.Enabled = value;
            }
        }

        public bool IsModal
        {
            get
            {
                return pIsModal;
            }
        }

        public AxWMPLib.AxWindowsMediaPlayer ManagedPlayer
        {
            get
            {
                return pMediaPlayer;
            }
        }

        public bool IsControlPanelVisible
        {
            get
            {
                return panel1.Visible;
            }
            set
            {
                panel1.Visible = value;
            }
        }

        #endregion

        #region UI Event Handlers

        void PlayerControl_VisibleChanged(object sender, EventArgs e)
        {
            if (ParentForm != null)
            {
                ParentForm.KeyPreview = true;
                ParentForm.KeyDown -= new KeyEventHandler(ParentForm_KeyDown);
                ParentForm.KeyDown += new KeyEventHandler(ParentForm_KeyDown);
            }
        }

        void ParentForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.KeyCode == Keys.Enter && pMediaPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                pMediaPlayer.fullScreen = !pMediaPlayer.fullScreen;
            }

        }


        void tbBar_ValueChanged(object sender, EventArgs e)
        {
            if (tbBar.Value != (int)(pMediaPlayer.Ctlcontrols.currentPosition / pMediaPlayer.Ctlcontrols.currentItem.duration * 100))
            {
                pMediaPlayer.Ctlcontrols.currentPosition = tbBar.Value * pMediaPlayer.Ctlcontrols.currentItem.duration / 100;
            }
        }

        void pMediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            switch (pMediaPlayer.playState)
            {
                case WMPLib.WMPPlayState.wmppsPlaying:
                    btPlay.Enabled = false;
                    btStop.Enabled = true;
                    btPause.Enabled = true;
                    mTimer.Start();
                    break;
                case WMPLib.WMPPlayState.wmppsPaused:
                    btPlay.Enabled = true;
                    btStop.Enabled = true;
                    btPause.Enabled = false;
                    mTimer.Start();
                    break;
                case WMPLib.WMPPlayState.wmppsStopped:
                    btPlay.Enabled = true;
                    btStop.Enabled = false;
                    btPause.Enabled = false;
                    mTimer.Start();
                    break;
                default:
                    btPlay.Enabled = false;
                    btStop.Enabled = false;
                    btPause.Enabled = false;
                    mTimer.Stop();
                    break;
            }
        }

        void btPause_Click(object sender, EventArgs e)
        {
            pMediaPlayer.Ctlcontrols.pause();
        }

        void btStop_Click(object sender, EventArgs e)
        {
            pMediaPlayer.Ctlcontrols.stop();
        }

        void btplay_Click(object sender, EventArgs e)
        {
            pMediaPlayer.Ctlcontrols.play();
        }

        #endregion

        #region Trackbar Updater

        void TrackbarUpdaterThread()
        {
            mTimer = new Timer();
            mTimer.Interval = 300;
            mTimer.Tick += new EventHandler(mTimer_Tick);
        }

        void mTimer_Tick(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(mTimer_Tick), sender, e);
                return;
            }

            tbBar.Value = (int)(pMediaPlayer.Ctlcontrols.currentPosition / pMediaPlayer.Ctlcontrols.currentItem.duration * 100);
        }

        #endregion

    }
}
